// ============================== 导入依赖 ============================== //

import Vue from 'vue'

// ============================== 导出计算属性 ============================== //

export default {
  // —————————————————————————————— 计算属性 —————————————————————————————— //

  /**
   * 设备树
   */
  deviceTree: ({ list, tree, param: { subSystemId } }) => {
    return transDeviceTree(tree, subSystemId, list)
  },
}

// ============================== 内部方法 ============================== //

/**
 * 转换设备树
 *
 * @param {Array} tree 结构树
 * @param {String} subSystemId 子系统 ID
 * @param {Array} list 当前区域的设备列表
 */
const transDeviceTree = (tree, subSystemId, list) => {
  tree.forEach((node) => {
    const deviceList = node[`device_${subSystemId}`]
    const showList = (deviceList || []).map((node) => ({
      nodes: [],
      id: node.id,
      name: node.deviceName,
      disabled: list.some((item) => item.deviceId === node.id),
      device: node,
    }))
    // 未加载过设备的占位空结点
    const emptyList = node.children.length || deviceList ? [] : [{ id: `${node.id}-empty`, deviceName: '' }]
    // 响应式设置
    Vue.set(node, 'nodes', [...showList, ...node.children, ...emptyList])
    transDeviceTree(node.children, subSystemId, list)
  })
  return tree
}
